import { http } from "@/utils/http";
import {
  SettlementQuery,
  SettlementRequest,
  SettlementVO,
  SettlementDetailVO,
  SettlementApproveRequest
} from "@/types/attendance/settlement";
import { ResponseData } from "@/types/common/response";

/**
 * 获取结算单列表
 * @param query 查询参数
 * @returns 结算单列表
 */
export function getSettlementList(
  query: SettlementQuery
): Promise<ResponseData<{ rows: SettlementVO[]; total: number }>> {
  // 过滤掉空字符串参数
  const filteredParams = Object.entries(query).reduce((acc, [key, value]) => {
    if (value !== "" && value !== null && value !== undefined) {
      acc[key] = value;
    }
    return acc;
  }, {} as Record<string, any>);

  // 确保分页参数存在
  const params = {
    ...filteredParams,
    pageNum: query.pageNum || 1,
    pageSize: query.pageSize || 10
  };

  return http.get<any, ResponseData<{ rows: SettlementVO[]; total: number }>>("/settlement/list", {
    params
  });
}

/**
 * 获取结算单详情
 * @param id 结算单ID
 * @returns 结算单详情
 */
export function getSettlementInfo(id: string | number): Promise<ResponseData<SettlementVO>> {
  return http.get<any, ResponseData<SettlementVO>>(`/settlement/${id}`);
}

/**
 * 获取结算单明细列表
 * @param id 结算单ID
 * @returns 结算单明细列表
 */
export function getSettlementDetailList(id: number): Promise<ResponseData<SettlementDetailVO[]>> {
  return http.get<any, ResponseData<SettlementDetailVO[]>>(`/settlement/detail/${id}`);
}

/**
 * 新增结算单
 * @param data 结算单数据
 * @returns 操作结果
 */
export function addSettlement(data: SettlementRequest): Promise<ResponseData<number>> {
  return http.post<any, ResponseData<number>>("/settlement", { data });
}

/**
 * 修改结算单
 * @param data 结算单数据
 * @returns 操作结果
 */
export function updateSettlement(data: SettlementRequest): Promise<ResponseData<void>> {
  return http.request("put", "/settlement", { data });
}

/**
 * 删除结算单
 * @param ids 结算单ID数组
 * @returns 操作结果
 */
export function deleteSettlement(ids: number | number[]): Promise<ResponseData<void>> {
  return http.request("delete", `/settlement/${ids}`);
}

/**
 * 提交审核
 * @param id 结算单ID
 * @returns 操作结果
 */
export function submitReview(id: number): Promise<ResponseData<void>> {
  return http.post<any, ResponseData<void>>(`/settlement/${id}/submit`, {});
}

/**
 * 审核结算单
 * @param data 审核信息
 * @returns 操作结果
 */
export function approveSettlement(data: SettlementApproveRequest): Promise<ResponseData<void>> {
  return http.post<any, ResponseData<void>>("/settlement/approve", { data });
}

/**
 * 导出结算单
 * @param query 查询参数
 * @returns 导出文件
 */
export function exportSettlement(query: SettlementQuery) {
  return http.get("/settlement/export", {
    params: query,
    responseType: "blob"
  });
}

/**
 * 计算结算单明细
 * @param data 计算参数
 * @returns 计算结果
 */
export function calculateSettlementDetails(data: {
  orderId: number;
  startDate: string;
  endDate: string;
  personnelIds: number[];
}): Promise<ResponseData<SettlementDetailVO[]>> {
  return http.post<any, ResponseData<SettlementDetailVO[]>>("/settlement/calculate", { data });
}

/**
 * 获取结算单号下拉选项
 * @returns 结算单号选项列表
 */
export function getBillNoOptions(): Promise<ResponseData<Array<{ billNo: string }>>> {
  return http.get<any, ResponseData<Array<{ billNo: string }>>>("/settlement/billno/options");
}
